package losrebellos.lospistoleros.components.preloader.sprite 
{
	import losrebellos.console.Console;
	import losrebellos.lospistoleros.components.application.sprite.base.BasePApplicationControllerSprite;
	import losrebellos.lospistoleros.components.preloader.IPPreloader;
	import losrebellos.lospistoleros.components.preloader.IPPreloaderController;
	import losrebellos.lospistoleros.components.preloader.IPPreloaderModel;
	import losrebellos.lospistoleros.core.model.sprite.IPModelSprite;
	import losrebellos.lospistoleros.core.view.sprite.IPViewSprite;
	import losrebellos.lospistoleros.data.content.Content;
	import losrebellos.lospistoleros.events.PPreloaderEvent;

	/*
	 *
	 * @author los rebellos - ben@benoitvinay.com
	 *
	 */
	public class PPreloaderControllerSprite extends BasePApplicationControllerSprite implements IPPreloaderController
	{
		/*
		 *
		 * CONSTRUCTOR
		 *
		 */
		public function PPreloaderControllerSprite(m:IPModelSprite, v:IPViewSprite = null, name:String = null, type:String = null)
		{
			super(m, v, name, type);
		}
		protected function get _preloaderModel():IPPreloaderModel
		{
			return _model as IPPreloaderModel;
		}
		protected function get _preloaderView():IPPreloader
		{
			return _view as IPPreloader;
		}

		
		/*
		 *
		 * INTERACTION
		 *
		 */
		override public function initialize():void
		{
			super.initialize();
			
			// set the language
			var lang:Object = _preloaderModel.getFlashVars(Content.LANGUAGE);
			if(lang)
			{
				_preloaderModel.lang = String(lang);
			}
			//
			
			// start the loading of the assets
			_preloaderModel.addEventListener(PPreloaderEvent.PROGRESS, progressAssetsXMLHandler);
			_preloaderModel.addEventListener(PPreloaderEvent.COMPLETE, completeAssetsXMLHandler);
			_preloaderModel.addEventListener(PPreloaderEvent.ERROR, errorAssetsXMLHandler);
			_preloaderModel.loadAssetsXML(_preloaderModel.applicationVO.requestAssets);
			//
		}
		override public function finalize():void
		{
			super.finalize();
			
			_preloaderModel.removeEventListener(PPreloaderEvent.PROGRESS, progressAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.COMPLETE, completeAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.ERROR, errorAssetsXMLHandler);
		}
		
		
		/*
		 * 
		 * HANDLERS ASSETS XML
		 * 
		 */
		protected function errorAssetsXMLHandler(e:PPreloaderEvent):void
		{
			_preloaderModel.removeEventListener(PPreloaderEvent.PROGRESS, progressAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.COMPLETE, completeAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.ERROR, errorAssetsXMLHandler);
		}
		protected function progressAssetsXMLHandler(e:PPreloaderEvent):void
		{
			
		}
		protected function completeAssetsXMLHandler(e:PPreloaderEvent):void
		{
			_preloaderModel.removeEventListener(PPreloaderEvent.PROGRESS, progressAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.COMPLETE, completeAssetsXMLHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.ERROR, errorAssetsXMLHandler);
			
			Console.simpleOutput("PPreloaderControllerSprite:completeAssetsXMLHandler");
			
			_preloaderModel.addEventListener(PPreloaderEvent.PROGRESS, progressAssetsHandler);
			_preloaderModel.addEventListener(PPreloaderEvent.COMPLETE, completeAssetsHandler);
			_preloaderModel.addEventListener(PPreloaderEvent.ERROR, errorAssetsHandler);
			_preloaderModel.loadAssets();
		}
		
		
		/*
		 * 
		 * HANDLERS ASSETS
		 * 
		 */
		protected function errorAssetsHandler(e:PPreloaderEvent):void
		{
			_preloaderModel.removeEventListener(PPreloaderEvent.PROGRESS, progressAssetsHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.COMPLETE, completeAssetsHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.ERROR, errorAssetsHandler);
		}
		protected function progressAssetsHandler(e:PPreloaderEvent):void
		{
			
		}
		protected function completeAssetsHandler(e:PPreloaderEvent):void
		{
			_preloaderModel.removeEventListener(PPreloaderEvent.PROGRESS, progressAssetsHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.COMPLETE, completeAssetsHandler);
			_preloaderModel.removeEventListener(PPreloaderEvent.ERROR, errorAssetsHandler);
			
			// add the index to the view
			_preloaderView.addIndex(_preloaderModel.getIndex());
			//
		}
	}
}